package com.ittx.dzt.shein.service.semi;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ittx.dzt.edi.constant.PlatformMessageConstant;
import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.edi.dto.StoreConfigDTO;
import com.ittx.dzt.edi.vo.ResponseMessageVO;
import com.ittx.dzt.log.monitor.ExceptionLog;
import com.ittx.dzt.shein.handler.AbstractBaseSheinSemiApiHandler;
import com.ittx.dzt.sys.response.Result;
import lombok.extern.slf4j.Slf4j;
import open.constant.PlatformApiConstant;
import open.dto.ShipmentDispatchDto;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 订单发货
 * <p>
 * cy.shipment.dispatch
 * 注意：上传运单号不代表“已发货”，SHEIN跟各大物流商打通，会根据承运商是否真实揽收作为订单发货状态的标准
 * /open-api/order/import-batch-multiple-express 批量上传运单号 https://open.sheincorp.com/zh/documents/apidoc/detail/3001274
 * </p>
 *
 * @author wl_code@163.com
 * @version 1.0
 * @date 2025/8/8
 */
@Slf4j
@Service
public class SheinSemiShipmentDispatchService extends AbstractBaseSheinSemiApiHandler<ShipmentDispatchDto> {
	@Override
	public String getSheinSemiMethod() {
		return "/open-api/order/import-batch-multiple-express";
	}

	@Override
	public String getLocalApiMethod() {
		return PlatformApiConstant.InboundInterface.SHIPMENT_DISPATCH;
	}

	@Override
	public Result<?> doProcess(PlatformApiParamDTO platformApiParamDTO, ShipmentDispatchDto shipmentDispatchDTO) {
		try {
			Map<String, Object> params = new HashMap<>();
			params.put("orderNo", shipmentDispatchDTO.getSourceOrderCode());
			List<Object> infoList = Lists.newArrayList();
			Optional.ofNullable(shipmentDispatchDTO.getPacks())
					.ifPresent(packList -> packList.forEach(pack -> {
						Map<String, Object> extendMap = pack.getExtendMap();
						Map<String, Object> waybillMap = Maps.newHashMap();
						// expressCode string	是 运单号；如物流商的运单号格式不对，则无法通过校验
						waybillMap.put("expressCode", pack.getWaybillCode());
						// expressIdCode string	是 商家可用物流公司，通过【查询发货渠道】获得
						waybillMap.put("expressIdCode", pack.getCarrierCode());
						// expressChannelCode string	否 渠道商产品编码，通过【查询发货渠道】获得
						if (StrUtil.isNotEmpty(MapUtil.getStr(extendMap, "expressChannelCode"))) {
							waybillMap.put("expressChannelCode", MapUtil.getStr(extendMap, "expressChannelCode"));
						}
						// goodsId bigint	是 商品唯一标识；同种商品多件，每件goodsId不同，通过订单详情接口获得
						waybillMap.put("goodsId", MapUtil.getLong(extendMap, "goodsId"));
						// status integer	是 是否删除运单号信息；1、根据goodsId，删除订单运单号信息；2、更新运单号信息
						waybillMap.put("status", MapUtil.getInt(extendMap, "status"));
						// goodExpressRemarkDto object	否 针对大件商品同sku发多个包裹的场景补充包裹运单信息时使用，注意并非所有商家都有该操作权限；

						Map goodExpressRemarkDto = MapUtil.get(extendMap, "goodExpressRemarkDto", Map.class);
						if (MapUtil.isNotEmpty(goodExpressRemarkDto)) {
							waybillMap.put("goodExpressRemarkDto", goodExpressRemarkDto);
						}
						infoList.add(waybillMap);
					}));
			params.put("infoList", infoList);

			StoreConfigDTO storeConfig = platformApiParamDTO.getStoreConfig();
			String requestId = platformApiParamDTO.getRequestId();
			Result<?> result = this.executeApi(storeConfig, params, requestId);
			if (result.hasError()) {
				return result;
			}
			Map<String, Object> resultMap = (Map<String, Object>) result.getData();
			List infos = MapUtil.get(resultMap, "info", List.class);
			return Result.success().addData(infos);
		} catch (Exception e) {
			String msg = StrUtil.format(PlatformMessageConstant.MSG_PLAT_0003_MSG, platformApiParamDTO.getRequestId(), this.getPlatformCode(), platformApiParamDTO.getApi(), e.getMessage());
			ExceptionLog.error(e, msg);
			return Result.error(new ResponseMessageVO(PlatformMessageConstant.MSG_PLAT_0003, msg));
		}
	}
}
